package cn.yuyin.item;



import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import cn.yuyin.item.Item;
import cn.yuyin.main.Main;
import cn.yuyin.page.InputBox;
import cn.yuyin.utile.FormatData;

public class TextBoxItem2 extends Item{
	String name;
	InputBox inputBox;
	Vector texts;

	public TextBoxItem2(int index,int x, int y, String name,String text,InputBox inputBox) {
		super(x, y, text);
		this.index = index;
		this.name = name;
		this.inputBox = inputBox;
		width = Main.WIDTH-Main.margin - 40;
		//width = img.getWidth()+Main.font.stringWidth(text)+130;
		height = Main.font.getHeight()+10;
		endX = this.x+width;
		endY = this.y;	
		inputBox.item = this;
	}
	public TextBoxItem2(int index,int x, int y, String name,String text) {
		super(x, y, text);
		this.index = index;
		this.name = name;
	
		width = Main.WIDTH-Main.margin - 40;
		//width = img.getWidth()+Main.font.stringWidth(text)+130;
		height = Main.font.getHeight()+10;
		endX = this.x+width;
		endY = this.y;	
	}
	
	public TextBoxItem2(int index,int x, int y, String name,String text,int height,InputBox inputBox) {
		super(x, y, text);
		this.index = index;
		this.name = name;
		this.inputBox = inputBox;
//		width = Main.WIDTH-Main.margin - 40;;
		width = Main.WIDTH-Main.margin-x*2;
		this.height = height;		
		endX = this.x+width;
		endY = this.y;	
		inputBox.item = this;
	}
	private void setIsHaveFouse(){
		if(index==Main.superPage.index){
			ishaveFouse = true;
		}else{
			ishaveFouse = false;
		}
	}

	public void paint(Graphics g) 
	{
		texts = FormatData.splitStr(Main.font, text, Main.WIDTH-Main.margin - 40-Main.font.stringWidth(name));
		int color = g.getColor();
		setIsHaveFouse();
		if(ishaveFouse)
		{
			//g.setColor(0xe7efff);
			//g.fillRect(x, y, Main.WIDTH-x*2, Main.font.getHeight()+10);
			
//			g.setColor(0xffffff);
//			g.fillRect(x, y, (Main.WIDTH-Main.margin-5*2)/2, height);
//			g.setColor(0xcecbce);
//			g.drawRect(x, y, (Main.WIDTH-Main.margin-5*2)/2, height);
			g.setColor(0xefefef);
			g.fillRect(x+1, y+1, Main.font.stringWidth(name)+15, height-1);
			
			
			g.setColor(0x6396e7);
			g.drawRect(x, y, Main.WIDTH/2+Main.margin+4, height);//(Main.WIDTH-Main.margin-5*2)/2
			g.drawRect(x+1, y+1, Main.WIDTH/2+Main.margin+4, height-2);//(Main.WIDTH-Main.margin-(5*2+2))/2

			g.setColor(0x395d9c);
			g.drawString(name, x+5, y+5, Graphics.LEFT|Graphics.TOP);
			g.setColor(0xbdbebd);
			for (int i = 0; i < texts.size(); i++) {
				if(5+Main.font.getHeight()*i+Main.font.getHeight()>height){
					break;
				}
				g.drawString(texts.elementAt(i).toString(), x+1+(Main.font.stringWidth(name)+15)+5, y+5+Main.font.getHeight()*i, Graphics.LEFT|Graphics.TOP);
			}
			
			

		}
		else
		{
			g.setColor(0xffffff);
			g.fillRect(x, y, Main.WIDTH/2+Main.margin+4, height);
			g.setColor(0xcecbce);
			g.drawRect(x, y, Main.WIDTH/2+Main.margin+4, height);
			g.setColor(0xefefef);
			g.fillRect(x+1, y+1, Main.font.stringWidth(name)+16, height-1);

			g.setColor(0x395d9c);
			g.drawString(name, x+5, y+5, Graphics.LEFT|Graphics.TOP);
			g.setColor(0xbdbebd);
			for (int i = 0; i < texts.size(); i++) {
				if(5+Main.font.getHeight()*i+Main.font.getHeight()>height){
					break;
				}
				g.drawString(texts.elementAt(i).toString(), x+1+(Main.font.stringWidth(name)+15)+5, y+5+Main.font.getHeight()*i, Graphics.LEFT|Graphics.TOP);
			}
		}
		
		g.setColor(color);
	}

	public void keyPressed(int keyCode) {
		// TODO Auto-generated method stub
		
	}

	public void keyReleased(int keyCode) {
		switch (keyCode) {
		case Main.key_OK:
			if(ishaveFouse)
			Main.display.setCurrent(inputBox);
			break;
		}
		
	}

	public void pointerPressed(int x, int y) {
		if(x>this.x && x<x+width && y>this.y && y<y+height){
			Main.display.setCurrent(inputBox);
		}
	}

	public void pointerReleased(int x, int y) {
		
	}

}
